package com.smgeek.gkrpc.server;


import com.smgeek.gkrpc.Request;
import com.smgeek.gkrpc.ServiceDescriptor;
import com.smgeek.gkrpc.common.utils.ReflectionUtils;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 管理rpc暴露的服务
 */

@Slf4j
public class ServiceManager {

    private Map<ServiceDescriptor,ServiceInstance> services;



    public ServiceManager(){
        this.services = new ConcurrentHashMap<>();
    }

    public <T> void register(Class<T> interfaceClass,T bean){
        Method[] methods = ReflectionUtils.getPublicMethods(interfaceClass);

        for(Method method: methods){
            ServiceInstance serviceInstance = new ServiceInstance(bean,method);
            ServiceDescriptor serviceDescriptor = ServiceDescriptor.from(interfaceClass,method);
            services.put(serviceDescriptor,serviceInstance);

            log.info("register service: {} {} ",serviceDescriptor.getClazz(),serviceDescriptor.getMethod());


        }
    }

    public ServiceInstance lookup(Request request){

        ServiceDescriptor service = request.getService();
        return services.get(service);


    }
}
